package pers.sloera.leetcode.editor.cn;

/**
 * 经营摩天轮的最大利润
 *
 * @author sloera
 * @date 2023-03-05 14:45:01
 */
class MaximumProfitOfOperatingACentennialWheel {
    public static void main(String[] args) {
        Solution solution = new MaximumProfitOfOperatingACentennialWheel().new Solution();
        System.out.println(solution.minOperationsMaxProfit(new int[]{8, 3}, 5, 6));
        System.out.println(solution.minOperationsMaxProfit(new int[]{10, 9, 6}, 6, 4));
        System.out.println(solution.minOperationsMaxProfit(new int[]{3, 4, 0, 5, 1}, 1, 92));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {
            int waitPerson = 0;
            int boardPerson;
            int maxProfit = 0;
            int ret = -1;
            int currentProfit = 0;
            // 等待人数不为0，或者customers不为空，都可能有更大的利润
            for (int i = 0; i < customers.length || waitPerson > 0; i++) {
                // 转动前人数
                if (i < customers.length) {
                    waitPerson += customers[i];
                }
                // 上摩人数
                boardPerson = waitPerson - Math.max(waitPerson - 4, 0);
                waitPerson = Math.max(waitPerson - 4, 0);
                if (boardPerson > 0) {
                    currentProfit += boardPerson * boardingCost;
                }
                // 每次转都需要花费成本
                currentProfit -= runningCost;
                if (currentProfit > maxProfit) {
                    maxProfit = currentProfit;
                    // i为0时，已经转了1次，所以转动次数比i多1
                    ret = i + 1;
                }
            }
            return ret;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)

}
